﻿using Axis.BusinessObject.Core;
using Axis.Dao.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Axis.Web.Views.Home
{
    public partial class ucNavigation : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ltrNavigation.Text = GetTopMenu();
            }
        }
        protected string GetTopMenu()
        {
            StringBuilder ret = new StringBuilder();
            string inputValue = "<InputValue UserID='' Session='' FunctionCode='TopMenu' />";
            CCoreDao cCoreDao = new CCoreDao();
            IList<CListData> list = cCoreDao.GetListData(inputValue);
            if (list != null && list.Count > 0)
            {
                foreach (CListData data in list)
                {
                    string item = "<li class='nav-[Code] menu_item' color='[Code]' menu-id='[ID]'><a href='[Link]' data-id='[ID]'><div class='border border-[Code]'></div><div class='menu_text'>[Name]</div></a>[SubMenu]</li>";
                    int ID = data.ID;
                    string subItem = string.Empty;
                    if (data.ID == 19 || data.ID == 23 || data.ID == 20)
                    {
                        subItem = GetSubMenu(data.ID, out ID);
                    }
                    item = item.Replace("[ID]", ID.ToString());
                    item = item.Replace("[Code]", data.Code);
                    item = item.Replace("[Name]", data.Name);
                    item = item.Replace("[Link]", data.Description);
                   
                    item = item.Replace("[SubMenu]", subItem);
                    ret.Append(item);
                }
            }
            return ret.ToString();
        }
        protected string GetSubMenu(int parentID, out int ID)
        {
            ID = parentID;
            StringBuilder ret = new StringBuilder();
            string inputValue = string.Format("<InputValue UserID='' Session='' FunctionCode='MenuCategory' ParentID='{0}' />", parentID);
            CCoreDao cCoreDao = new CCoreDao();
            IList<CListData> list = cCoreDao.GetListData(inputValue);
            if (list != null && list.Count > 0)
            {
                ret.AppendFormat("<ul class='sub_menu' parentID='{0}'>", parentID);
                
                    ID = list[0].ID;
                foreach (CListData data in list)
                {
                    string href = string.Empty;
                    switch (data.ParentID)
                    {
                        case 19:
                            href = "/Views/TinTucKhuyenMai/";
                            break;
                        case 20:
                            href = "/Views/Product/TopProduct.aspx?ID=";
                            break;
                        case 21:
                            href = "";
                            break;
                        case 22:
                            href = "";
                            break;
                        case 23:
                            href = "/Views/YKienTieuDung/";
                            break;
                    }
                    if (string.IsNullOrEmpty(href))
                    {
                        href = "javascript:void(0);";
                    }
                    else
                    {
                       
                        href = href  + data.ID;
                    }
                    ret.AppendFormat("<li><a href='{0}' data-id='{1}'>{2}</a></li>", href,data.ID, data.Name);
                }
                ret.Append("</ul>");
            }
            return ret.ToString();
        }
    }
}